package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.Paint
import android.text.Layout
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.core.view.setPadding
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsIndex
import client.comm.baoding.databinding.LayoutJxhwItemBinding
import client.comm.baoding.ui.GoodsDetailActivity
import client.comm.baoding.utils.TextTool
import client.comm.commlib.base.BaseListAdapter
import client.comm.commlib.base.BindingViewHoder
import client.comm.commlib.dipToPx
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.youth.banner.adapter.BannerAdapter

/**
 * 精选商品
 */
class JxGood2Adapter(val context: Context, val manager: RequestManager) : BannerAdapter<GoodsIndex.JxGoods, RecyclerView.ViewHolder>(null) {


    override fun onCreateHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder {

        return BindingViewHoder(
            DataBindingUtil.inflate(
                LayoutInflater.from(context),
                R.layout.layout_jxhw_item,
                parent,
                false
            )
        )
    }

    override fun onBindView(holder: RecyclerView.ViewHolder?, data: GoodsIndex.JxGoods?, position: Int, size: Int) {
        val bindingViewHolder = holder as BindingViewHoder<ViewDataBinding>
        val binding = bindingViewHolder.binding as LayoutJxhwItemBinding
        val bean = getData(position)
        binding.bean = bean
        binding.event = this

        manager.load(bean.goods_img)
//            .apply(RequestOptions().error(R.mipmap.ic_launcher))
//            .thumbnail(0.5f)

//            .skipMemoryCache(true) //跳过内存缓存
//            .diskCacheStrategy(DiskCacheStrategy.NONE) //不缓冲disk硬盘中
            .into(binding.img)

//        TextTool.getBuilder("¥").setAlign(Layout.Alignment.ALIGN_CENTER).setBold().setTextSize(14)
//            .append(bean.price.toString()).setForegroundColor(Color.parseColor("#E81837")).setBold().setTextSize(18)
//            .into(binding.tvPrice)

        if (bean.price > 0) {
            binding.tvPrice.text = "￥${bean.price}"
        } else if (bean.integral > 0) {
            binding.tvPrice.text = "积分${bean.integral}"
        }
        binding.llLabel.removeAllViews()
        for (e in bean.goods_label) {
            binding.llLabel.addView(getLabel(e))
        }
    }


    fun itemClick(bean: GoodsIndex.JxGoods) {
        context.startActivity(Intent(context, GoodsDetailActivity::class.java).apply {
            putExtra("goodsId", "${bean.goods_id}")
        })
    }

    //右边按钮点击事件
    private fun getLabel(str: String): View? {
        val child = TextView(context)
        child.textSize = 12f
        var drawId = when (str) {
            "热卖" -> R.drawable.label_bg_red1_0
            "新品" -> R.drawable.label_bg_green1_0
            "爱心商品" -> R.drawable.label_bg_blue1_0
            else -> null
        }
        if (drawId != null) {
            child.background = context.getDrawable(drawId)
        }

        child.setTextColor(context.resources.getColor(R.color.white))
        child.setText(str)
        child.setPadding(dipToPx(context.resources, 2f).toInt())
        // 定义LayoutParam
        val params: RelativeLayout.LayoutParams =
            RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        params.rightMargin = dipToPx(context.resources, 10f).toInt()
        child.layoutParams = params
        // 调用带有LayoutParams参数的addView方法
        return child//mContainer.addView(child, params)
    }

    fun submitList(d: List<GoodsIndex.JxGoods>) {
        setDatas(d)
    }
}